package targetToOffer.eigthDay;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Godc
 * @description
 */
public class TwoSum {
}
class TwoSumSolution {
    public int[] twoSum(int[] nums, int target) {
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            if(nums[i]>=target) break;
            if(set.contains(target-nums[i])) return new int[]{nums[i],target-nums[i]};
            set.add(nums[i]);
        }
        return null;
    }
    /**
     * 没有充分利用递增的性质，对撞双指针
     */
    public int[] twoSum1(int[] nums, int target){
        int i = 0,j = nums.length-1;
        while (i<j){
            int sum = nums[i]+nums[j];
            if(sum==target) return new int[]{nums[i],nums[j]};
            else if(sum>target) j--;
            else i++;
        }
        return new int[]{};
    }
}